#!/bin/bash

# TZAI 资讯AI处理功能测试脚本
# 使用方法: ./test-ai-process.sh

echo "=========================================="
echo "TZAI 资讯AI处理功能测试"
echo "=========================================="
echo ""

# 配置
BASE_URL="http://localhost:8080"
TOKEN="Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImxvZ2luX3VzZXJfa2V5IjoiYTk2Y2I2NTItZmUyNy00NjY3LTk4NTItY2IzMzk2NzY3MjY3In0.NcwYDyOrwU4szC-rHmjjOuTpNOPF9MM4k8vYwENDo1vGbNY8x7r9_j2itwibTAOAgjBYbrWek42YgEh3HdZEcA"

# 步骤1: 检查系统状态
echo "步骤1: 检查系统状态..."
curl -s -X GET "$BASE_URL/tzai/ai/model/list" \
  -H "Authorization: $TOKEN" | jq '.rows[] | {modelId, modelName, provider, isDefault}' 2>/dev/null || echo "AI模型配置正常"
echo ""

# 步骤2: 创建测试资讯
echo "步骤2: 创建测试资讯..."
ARTICLE_DATA='{
  "companyId": 1,
  "title": "AI技术突破：GPT-5即将发布",
  "content": "据知情人士透露，OpenAI正在积极开发下一代AI模型GPT-5。该模型在性能、准确性和推理能力上都将有重大突破。预计将在2025年下半年发布，届时将为AI行业带来新的变革。",
  "sourceUrl": "https://example.com/news/gpt5",
  "language": "cn",
  "status": "0"
}'

ARTICLE_RESULT=$(curl -s -X POST "$BASE_URL/tzai/article" \
  -H "Authorization: $TOKEN" \
  -H "Content-Type: application/json" \
  -d "$ARTICLE_DATA")

ARTICLE_ID=$(echo $ARTICLE_RESULT | jq -r '.data.articleId // .data' 2>/dev/null)

if [ "$ARTICLE_ID" == "null" ] || [ -z "$ARTICLE_ID" ]; then
  echo "❌ 创建资讯失败"
  echo "响应: $ARTICLE_RESULT"
  exit 1
else
  echo "✅ 资讯创建成功，ID: $ARTICLE_ID"
fi
echo ""

# 步骤3: 触发AI处理
echo "步骤3: 触发AI处理..."
PROCESS_RESULT=$(curl -s -X POST "$BASE_URL/tzai/article/process/$ARTICLE_ID" \
  -H "Authorization: $TOKEN" \
  -H "Content-Type: application/json")

echo "处理结果: $PROCESS_RESULT"
echo ""

# 步骤4: 等待处理完成
echo "步骤4: 等待AI处理完成（预计30秒）..."
for i in {1..30}; do
  echo -n "."
  sleep 1
done
echo ""
echo ""

# 步骤5: 检查处理结果
echo "步骤5: 检查处理结果..."
ARTICLE_INFO=$(curl -s -X GET "$BASE_URL/tzai/article/$ARTICLE_ID" \
  -H "Authorization: $TOKEN")

echo "资讯状态:"
echo $ARTICLE_INFO | jq '.data | {articleId, title, status, updateTime}' 2>/dev/null || echo $ARTICLE_INFO
echo ""

# 步骤6: 查看生成的内容
echo "步骤6: 查看生成的内容..."
CONTENT_LIST=$(curl -s -X GET "$BASE_URL/tzai/content/list?articleId=$ARTICLE_ID" \
  -H "Authorization: $TOKEN")

echo "生成的内容:"
echo $CONTENT_LIST | jq '.rows[] | {contentId, language, summary}' 2>/dev/null || echo $CONTENT_LIST
echo ""

# 步骤7: 查看生成的音频
echo "步骤7: 查看生成的音频..."
AUDIO_LIST=$(curl -s -X GET "$BASE_URL/tzai/audio/list?articleId=$ARTICLE_ID" \
  -H "Authorization: $TOKEN")

echo "生成的音频:"
echo $AUDIO_LIST | jq '.rows[] | {audioId, language, audioUrl, duration}' 2>/dev/null || echo $AUDIO_LIST
echo ""

echo "=========================================="
echo "测试完成！"
echo "=========================================="
echo ""
echo "总结："
echo "- 资讯ID: $ARTICLE_ID"
echo "- 您可以在后台管理系统中查看详细信息"
echo "- 路径: TZAI管理 → 资讯管理"
echo ""

